package dryworks.jsf.examples.generator;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;

import dryworks.examples.create.model.CreateExampleTask;

public class ExampleGenerator {
	CreateExampleTask createExampleTask;

	String exampleManagedBeanPackageCommonPart = "drytools.icefaces.examplesbeans";
	String exampleResourcesPackageCommonPart = "drytools.icefaces.examples.nl";

	public ExampleGenerator(CreateExampleTask createExampleTask) {
		this.createExampleTask = createExampleTask;
	}

	public CreateExampleTask getCreateExampleTask() {
		return createExampleTask;
	}

	public void setCreateExampleTask(CreateExampleTask createExampleTask) {
		this.createExampleTask = createExampleTask;
	}

	public String calculateResourceBundleMainName() {
		return GeneratorStringUtils.upFirstLetter(getCreateExampleTask().getName());
	}
	
	public String calculateResourceBundleFullMainName() {
		return getExampleResourcesPackageCommonPart()+"."+getCreateExampleTask().getCategory()+"."+getCreateExampleTask().getName()+"."+calculateResourceBundleMainName();

	}
	public String calculateResourceBundleFullMainName(String name) {
		return getExampleResourcesPackageCommonPart()+"."+getCreateExampleTask().getCategory()+"."+getCreateExampleTask().getName()+"."+name;
	}
	public String generateResourceBundleContent() {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(buffer);
		
		out.println("example.title=");
		out.println("example.shortDescription=");
		out.println("example.longDescription=");
		
		out.flush();
		return buffer.toString();
	}
	
	public String generateJspContentForElementOnly() {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(buffer);

		out
				.println("<!-- x:comment This example explains how to build and customize ... -->");
		out.println("<h:panelGrid xmlns:h=\"http://java.sun.com/jsf/html\"");
		out.println("xmlns:f=\"http://java.sun.com/jsf/core\"");
		out.println("xmlns:ice=\"http://www.icesoft.com/icefaces/component\">");
		out
		.println("<!-- x:line  -->");
		
		out.println("</h:panelGrid>");

		out.flush();
		return buffer.toString();
	}
	
//	public String generateJspContentForSeparateFile() {
//		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
//		PrintStream out = new PrintStream(buffer);
//
//		out
//				.println("<!-- x:comment This example explains how to build and customize ... -->");
//		out.println("<f:view xmlns:h=\"http://java.sun.com/jsf/html\"");
//		out.println("xmlns:f=\"http://java.sun.com/jsf/core\"");
//		out.println("xmlns:ice=\"http://www.icesoft.com/icefaces/component\">");
//		out.println("<ice:outputDeclaration doctypeRoot=\"HTML\"");
//		out.println("doctypePublic=\"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
//		out
//				.println("doctypeSystem=\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"/>");
//		out.println("<ice:outputHtml>");
//		out.println("<ice:outputBody>");
//		out.println("<ice:outputStyle href='./xmlhttp/css/xp/xp.css'/>");
//		out.println("<ice:form>");
//		out.println("</ice:form>");
//		out.println("</ice:outputBody>");
//		out.println("</ice:outputHtml>");
//		out.println("</f:view>");
//
//		out.flush();
//		return buffer.toString();
//	}

	
	public String generateManagedBeanContent() {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(buffer);

		out.println("package " + calculatePackageName() + ";");

		out.println("import java.io.IOException;");

		out.println("public class " + calculateManagedBeanShortClassName()
				+ " {");
		out.println("public " + calculateManagedBeanShortClassName() + "() {}");

		out.println("} // end of class ");

		out.flush();
		return buffer.toString();
	}

	public String calculatePackageName() {
		return getExampleManagedBeanPackageCommonPart() + "."
				+ getCreateExampleTask().getName();
	}

	public String calculateManagedBeanShortClassName() {
		return GeneratorStringUtils.upFirstLetter(createExampleTask.getName())
				+ "Bean";
	}

	public String calculateManagedBeanFullClassName() {
		return calculatePackageName() + "."
				+ calculateManagedBeanShortClassName();
	}

	public String calculateFileNameForFacesConfigSubFile() {
		return ""
				+ GeneratorStringUtils.upFirstLetter(createExampleTask
						.getName()) + ".xml1";
	}

	public String calculateEntityNameForFacesConfigSubFile() {
		return createExampleTask.getName();
	}

	public String generateManagedBeanDefinition() {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(buffer);
		
		out.println("<managed-bean>");
		out.println("  <managed-bean-name>"
				+ createExampleTask.getName() + "Bean</managed-bean-name>");
		
		out.println("  <managed-bean-class>"+calculateManagedBeanFullClassName()+"</managed-bean-class>");

		out.println("  <managed-bean-scope>session</managed-bean-scope>");
		out.println("</managed-bean>");
		
		out.flush();
		return buffer.toString();
	}

	public String getExampleManagedBeanPackageCommonPart() {
		return exampleManagedBeanPackageCommonPart;
	}

	public void setExampleManagedBeanPackageCommonPart(
			String exampleManagedBeanPackageCommonPart) {
		this.exampleManagedBeanPackageCommonPart = exampleManagedBeanPackageCommonPart;
	}

	public String getExampleResourcesPackageCommonPart() {
		return exampleResourcesPackageCommonPart;
	}

	public void setExampleResourcesPackageCommonPart(
			String exampleResourcesPackageCommonPart) {
		this.exampleResourcesPackageCommonPart = exampleResourcesPackageCommonPart;
	}
}
